package bavaria.hightech.banking.money;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Properties;

import bavaria.hightech.banking.money.Currency.currencyEnum;

/**
 * @author Dietmar contains balance and some money methods
 */
public class Money {

	public BigInteger balance = new BigInteger("0");

	public currencyEnum act_currency;

	/**
	 * default-constructor
	 * @param currency
	 */
	public Money(currencyEnum currency) {
		act_currency = currency;
	}

	/**
	 * convert from one currency to another currency
	 * @param currencyToProcess
	 * @param amount
	 * @return
	 */
	private BigInteger convertCurrency(currencyEnum currencyToProcess,
			BigInteger amount) {
		BigInteger tmp = amount;
		Float euro2dollar, dollar2euro;
		Properties properties = new Properties();
		if (currencyToProcess == act_currency) {
			return amount;
		}
		try {
			properties.load(new FileInputStream(
					"src/bavaria/hightech/banking/bank/convert.properties"));
		} catch (IOException e) {
		}
		if (currencyToProcess == currencyEnum.DOLLAR
				&& act_currency == currencyEnum.EURO) {
			dollar2euro = Float.parseFloat((String) properties
					.get("DOLLAR2EURO"));
			tmp = new BigInteger(""
					+ (long) (balance.add(amount).intValue() * dollar2euro));
		} else if (currencyToProcess == currencyEnum.EURO
				&& act_currency == currencyEnum.DOLLAR) {
			euro2dollar = Float.parseFloat((String) properties
					.get("EURO2DOLLAR"));
			tmp = new BigInteger(""
					+ (long) (balance.add(amount).intValue() * euro2dollar));
		} else {
			throw new UnsupportedOperationException(
					"conversion check in the money class failed. At least one specified currency is invalid!");
		}
		return tmp;
	}

	/**
	 * decrease the amount
	 * @param currency
	 * @param amount
	 */
	public void drawAmount(currencyEnum currency, BigInteger amount) {
		BigInteger converted = convertCurrency(currency, amount);
		BigInteger newBalance = new BigInteger(""
				+ balance.subtract(converted).intValue());
		balance = newBalance;
	}

	/**
	 * increase the amount
	 * @param currency
	 * @param amount
	 */
	public void addAmount(currencyEnum currency, BigInteger amount) {
		BigInteger converted = convertCurrency(currency, amount);
		BigInteger newBalance = new BigInteger(""
				+ balance.add(converted).intValue());
		balance = newBalance;
	}

	/**
	 * @param amount
	 *            value to compare
	 * @return true when amount equals balance
	 */
	public boolean isEqualTo(BigInteger amount) {
		if (balance.intValue() == amount.intValue()) {
			return true;
		}
		return false;
	}

	/**
	 * @param amount
	 *            value to compare
	 * @return true when balance is greater than amount
	 */
	public boolean isGreaterThan(BigInteger amount) {
		if (balance.intValue() > amount.intValue()) {
			return true;
		}
		return false;
	}

	/**
	 * get the balance
	 * @return
	 */
	public long getBalance() {
		return balance.intValue();
	}
}
